﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.Gxt;
using Hjt212Parser;
using log4net;

namespace Biz.Reissue
{
    public class DataLostCmdGen
    {
        private static ILog Log = LogManager.GetLogger(typeof(DataLostCmdGen));
        public static void GenCmd()
        {
            var now = DateTime.Now;
            var endHour = new DateTime(now.Year, now.Month, now.Day, now.Hour, 0, 0).AddHours(-1);
            var beginHour = endHour.AddHours(-1);

            var endDay = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0).AddDays(-1);
            var beginDay = endDay.AddDays(-1);

            using (var pCtxGxt = new PlatformGxtContext())
            using (var gCtx = new GatewayMonitorContext())
            {
                try
                {
                    var mns = pCtxGxt.companyinfoes.Select(t => t.GasMN).Distinct().ToList();
                    foreach (var mn in mns)
                    {
                        if (string.IsNullOrEmpty(mn)) continue;
                        var datatimes =
                            pCtxGxt.xaxis_hour.Where(t => t.MN.Equals(mn)).Where(t=>t.datatime>= beginHour&&t.datatime<=endHour)
                                .Select(t => t.datatime)
                                .Distinct()
                                .ToList();
                        var lostDatatimes = DataLostAlgorithm.CheckData(datatimes, beginHour, endHour, DataTypeEnum.Hour);

                        foreach (var time in lostDatatimes)
                        {
                            var st = "31";
                            var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            var temp = "QN=" + qn + ";ST=" + st + ";CN=2061;PW=123456;MN=" + mn + ";Flag=1;CP=&&BeginTime=" +
                                       time.ToString("yyyyMMddHHmmss") + ",EndTime=" +
                                       time.ToString("yyyyMMddHHmmss") + "&&";
                            temp = Crc.WrapByCrc(temp, "monitor");

                            var item = new send_data
                            {
                                mn = mn,
                                cn = "2061",
                                is_sent = false,
                                qn = qn,
                                send_time = DateTime.Now,
                                sent_time = DateTime.Now,
                                send_string = temp
                            };
                            gCtx.send_data.Add(item);
                            gCtx.SaveChanges();
                        }

                        datatimes =
                           pCtxGxt.xaxis_day.Where(t => t.MN.Equals(mn)).Where(t => t.datatime != null).Where(t => t.datatime >= beginDay && t.datatime <= endDay)
                               .Select(t => t.datatime.Value)
                               .Distinct()
                               .ToList();
                        lostDatatimes = DataLostAlgorithm.CheckData(datatimes, beginDay, endDay, DataTypeEnum.Day);

                        foreach (var time in lostDatatimes)
                        {
                            var st = "31";
                            var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            var temp = "QN=" + qn + ";ST=" + st + ";CN=2031;PW=123456;MN=" + mn + ";Flag=1;CP=&&BeginTime=" +
                                       time.ToString("yyyyMMddHHmmss") + ",EndTime=" +
                                       time.ToString("yyyyMMddHHmmss") + "&&";
                            temp = "##" + temp.Length.ToString("D4") + temp + Crc.WrapByCrc(temp, "monitor") + "\r\n";

                            var item = new send_data
                            {
                                mn = mn,
                                cn = "2031",
                                is_sent = false,
                                qn = qn,
                                send_time = DateTime.Now,
                                sent_time = DateTime.Now,
                                send_string = temp
                            };
                            gCtx.send_data.Add(item);
                            gCtx.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }

                try
                {
                    var mns = pCtxGxt.companyinfoes.Select(t => t.GasMN).Distinct().ToList();
                    foreach (var mn in mns)
                    {
                        if (string.IsNullOrEmpty(mn)) continue;
                        var datatimes =
                            pCtxGxt.xaxis_hour.Where(t => t.MN.Equals(mn)).Where(t => t.datatime >= beginHour && t.datatime <= endHour)
                                .Select(t => t.datatime)
                                .Distinct()
                                .ToList();
                        var lostDatatimes = DataLostAlgorithm.CheckData(datatimes, beginHour, endHour, DataTypeEnum.Hour);

                        foreach (var time in lostDatatimes)
                        {
                            var st = "32";
                            var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            var temp = "QN=" + qn + ";ST=" + st + ";CN=2061;PW=123456;MN=" + mn + ";Flag=1;CP=&&BeginTime=" +
                                       time.ToString("yyyyMMddHHmmss") + ",EndTime=" +
                                       time.ToString("yyyyMMddHHmmss") + "&&";
                            temp = "##" + temp.Length.ToString("D4") + temp + Crc.WrapByCrc(temp, "monitor") + "\r\n";

                            var item = new send_data
                            {
                                mn = mn,
                                cn = "2061",
                                is_sent = false,
                                qn = qn,
                                send_time = DateTime.Now,
                                sent_time = DateTime.Now,
                                send_string = temp
                            };
                            gCtx.send_data.Add(item);
                            gCtx.SaveChanges();
                        }

                        datatimes =
                           pCtxGxt.xaxis_day.Where(t => t.MN.Equals(mn)).Where(t => t.datatime != null).Where(t => t.datatime >= beginDay && t.datatime <= endDay)
                               .Select(t => t.datatime.Value)
                               .Distinct()
                               .ToList();
                        lostDatatimes = DataLostAlgorithm.CheckData(datatimes, beginDay, endDay, DataTypeEnum.Day);

                        foreach (var time in lostDatatimes)
                        {
                            var st = "32";
                            var qn = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            var temp = "QN=" + qn + ";ST=" + st + ";CN=2031;PW=123456;MN=" + mn + ";Flag=1;CP=&&BeginTime=" +
                                       time.ToString("yyyyMMddHHmmss") + ",EndTime=" +
                                       time.ToString("yyyyMMddHHmmss") + "&&";
                            temp = "##" + temp.Length.ToString("D4") + temp + Crc.WrapByCrc(temp, "monitor") + "\r\n";

                            var item = new send_data
                            {
                                mn = mn,
                                cn = "2031",
                                is_sent = false,
                                qn = qn,
                                send_time = DateTime.Now,
                                sent_time = DateTime.Now,
                                send_string = temp
                            };
                            gCtx.send_data.Add(item);
                            gCtx.SaveChanges();
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }
        }
    }
}
